package studentsinfomanagement.view;

import studentsinfomanagement.bean.Student;
import studentsinfomanagement.bean.Teacher;
import studentsinfomanagement.bean.User;
import studentsinfomanagement.main.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Enumeration;

/**
 * 注册界面类
 */
public class RegisterView extends JFrame {

    private Main main = null;
    private RegisterView registerView = null;

    public RegisterView(Main main){
        this.main = main;
        registerView = this;

        this.setTitle("注册账号");//标题
        this.setSize(350,300);//界面大小
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//关闭界面取消注册
        this.setLocationRelativeTo(null);//界面默认居中
        this.setResizable(false);//界面大小不可调节
        FlowLayout fl = new FlowLayout();//流式布局
        this.setLayout(fl);

        JLabel jl_name = new JLabel("姓名");
        JLabel jl_acc = new JLabel("账号");
        JLabel jl_dep = new JLabel("专业");
        JLabel jl_sex = new JLabel("性别");
        JLabel jl_pwd = new JLabel("密码");

        JTextField jt_name = new JTextField();//姓名输入框
        jt_name.setPreferredSize(new Dimension(280,30));
        JTextField jt_acc = new JTextField();//账号输入框
        jt_acc.setPreferredSize(new Dimension(280,30));
        JTextField jt_dep = new JTextField();//专业输入框
        jt_dep.setPreferredSize(new Dimension(280,30));
        JComboBox<String> jcom_sex = new JComboBox<>();//选择性别
        jcom_sex.addItem("男");
        jcom_sex.addItem("女");
        JPasswordField jp_pwd = new JPasswordField();//密码输入框
        jp_pwd.setPreferredSize(new Dimension(280,30));

        ButtonGroup btn_grp = new ButtonGroup();
        JRadioButton jr_teacher = new JRadioButton("老师");
        JRadioButton jr_student = new JRadioButton("学生");
        btn_grp.add(jr_teacher);
        btn_grp.add(jr_student);

        JButton next_btn = new JButton("确认");//确认按钮
        next_btn.setPreferredSize(new Dimension(250,25));
        JButton back_btn = new JButton("返回");//取消按钮
        back_btn.setPreferredSize(new Dimension(250,25));

        this.add(jl_name);
        this.add(jt_name);
        this.add(jl_acc);
        this.add(jt_acc);
        this.add(jl_dep);
        this.add(jt_dep);
        this.add(jl_pwd);
        this.add(jp_pwd);
        this.add(jl_sex);
        this.add(jcom_sex);
        this.add(jr_teacher);
        this.add(jr_student);
        this.add(next_btn);
        this.add(back_btn);

        this.setVisible(true);//设置界面可见

        //添加监听器
        //确认
        next_btn.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String name = jt_name.getText().trim();
                String acc = jt_acc.getText().trim();
                String department = jt_dep.getText().trim();
                int index_sex = jcom_sex.getSelectedIndex();
                String sex = index_sex == 0?"M":"F";
                String pwd = new String(jp_pwd.getPassword()).trim();
                String s_id = null;
                Enumeration<AbstractButton> radioBtns = btn_grp.getElements();
                while (radioBtns.hasMoreElements()) {
                    AbstractButton btn = radioBtns.nextElement();
                    if (btn.isSelected()) {
                        s_id = btn.getText();
                        break;
                    }
                }
                //2.判断信息是否完整
                if (acc.equals("") || acc.equals("") || department.equals("") || pwd.equals("") || s_id == null) {
                    JOptionPane.showMessageDialog(registerView, "请输入完整！");
                } else {
                    User user = null;
                    int type;
                    if (s_id.equals("老师")) {//老师
                        type = 1;
                        Teacher teacher = new Teacher();
                        teacher.setName(name);
                        teacher.setTno(acc);
                        teacher.setDepartment(department);
                        teacher.setSex(sex);
                        teacher.setPassword(pwd);
                        user = teacher;
                    } else {//学生
                        type = 0;
                        Student student = new Student();
                        student.setName(name);
                        student.setSno(acc);
                        student.setDepartment(department);
                        student.setSex(sex);
                        student.setPassword(pwd);
                        user = student;
                    }
                    //调用逻辑层的注册方法
                    boolean result = main.register(user, type);
                    if (!result) {//注册失败
                        JOptionPane.showMessageDialog(registerView, "注册失败，请检查您的账号和专业号！");
                    }else {
                        JOptionPane.showMessageDialog(registerView, "注册成功！");
                        registerView.setVisible(false);//退出注册界面
                    }
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        //取消
        back_btn.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                registerView.setVisible(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

    }

}
